package com.example.ui.leftmenu;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.eton.czh.GifView;
import com.example.huashangapp.BaseView;
import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;

import com.example.service.MainService;
import com.example.ui.MainActivity;
import com.example.ui.learn.CurriculumCenterActivity;
import com.example.ui.learn.ListeningActivity;
import com.example.ui.learn.ListeningInfo;
import com.example.ui.learn.util.CurriculumCenter;
import com.example.ui.learn.util.MyListViewAdapter1;
import com.example.util.AsyncLoadImage;
import com.example.util.RefreshableListView;
import com.example.util.RefreshableListView.OnRefreshListener;
import com.example.util.Util;
import com.hp.hpl.sparta.Text;

import android.app.AlertDialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @Description 选项1
 */
public class Item01View extends BaseView {
	private Context context;
	private MyListViewAdapter1 item_adapter;
	private ArrayList<CurriculumCenter> item1_cc=new ArrayList<CurriculumCenter>();
	private Button titleLeftBtn, titleRightBtn;
	private TextView titleText;
	private String data;
	private RefreshableListView mListView;
	AlertDialog infoad;
	AlertDialog.Builder bulider;
	public Item01View(Context context) {
		this.context = context;
	}
	Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				infoad.dismiss();
				Util.toast(context,"数据提取异常请重试！");
				break;
			case 1:
				infoad.dismiss();
				item_adapter.setCcInfos(item1_cc);
				item_adapter.notifyDataSetChanged();
				break;
			case 2:
				Util.toast(context, "提取异常请下拉刷新数据");
				break;
			default:
				break;
			}
		};
	};
	Runnable item1_run = new Runnable() {
		@Override
		public void run() {
			data = NetRequest.Request_Item01();
			JSONObject j;
			try {
				j = new JSONObject(data);
				String jsondate= j.getString("response");
				if(jsondate.equals("true")){
					item1_cc=AnalyzingJson.getdata_item1(data);
					HSApplication.item1_cc = item1_cc;
					h.sendEmptyMessage(1);
				}else{
					h.sendEmptyMessage(2);
				}
			} catch (Exception e) {
				h.sendEmptyMessage(0);
				e.printStackTrace();
			}
		}
	};
	private OnClickListener i1_ocl = new OnClickListener() {
		@Override
		public void onClick(View v) {
		
			switch (v.getId()) {
			case R.id.layout_title_left_btn:
				myMoveView.showHideLeftMenu();
				break;
			case R.id.layout_title_right_btn:
				if(HSApplication.HSlistInfo.getListen_uri()==null||"".equals(HSApplication.HSlistInfo.getListen_uri())){
					Toast.makeText(context, "目前您还没有收听过任何课程!",Toast.LENGTH_SHORT).show();
					return;
				}
				Intent i1_l = new Intent();
				i1_l.setClass(context, ListeningActivity.class);
				Bundle lisinfo=new Bundle();
				lisinfo.putParcelable(ListeningActivity.LISTEN,HSApplication.HSlistInfo);
				i1_l.putExtras(lisinfo);
				i1_l.putExtra(ListeningActivity.ISLISTENING,true);
				context.startActivity(i1_l);
				break;
			default:
				break;
			}
		}
	};
	private GifView mygifView;

	public void init() {
		view = LayoutInflater.from(context).inflate(R.layout.item01, null);
		titleLeftBtn = (Button) view.findViewById(R.id.layout_title_left_btn);
		titleRightBtn = (Button) view.findViewById(R.id.layout_title_right_btn);
		titleText = (TextView) view.findViewById(R.id.layout_title_middle_text);
		item_adapter = new MyListViewAdapter1(context,item1_cc,
				new AsyncLoadImage());
		mListView = (RefreshableListView) view
				.findViewById(R.id.item01_listview);
		mListView.setAdapter(item_adapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				Intent intent = new Intent(context,
						CurriculumCenterActivity.class);
				intent.putExtra("id", item1_cc.get(position).getId() + "");
				intent.putExtra("title",item1_cc.get(position).getTitle());
				context.startActivity(intent);
			}
		});
		mListView.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh(RefreshableListView listView) {
				new NewDataTask().execute();
			}
		});

		titleLeftBtn.setText("菜单");
		titleText.setText("选课中心");
		titleRightBtn.setText("正在听");
		Resources r = context.getResources(); 
		int btn_h = (int) r.getDimension(R.dimen.ting_left);
		int btn_left=(int)r.getDimension(R.dimen.text_left);
		titleRightBtn.setPadding(btn_left, 0,  btn_h, 0);
		mygifView = (GifView)view.findViewById(R.id.testView);
		mygifView.setGifImage(R.drawable.ting);
		mygifView.setVisibility(0);
		titleLeftBtn.setOnClickListener(i1_ocl);
		titleRightBtn.setOnClickListener(i1_ocl);
		//初始化数据
		getData();
	}
	public void getData() {
		if(HSApplication.item1_cc.size()>0){
			item1_cc=HSApplication.item1_cc;
			item_adapter.setCcInfos(item1_cc);
			item_adapter.notifyDataSetChanged();
			}else{
				bulider = new AlertDialog.Builder(context);
				bulider.setView(View.inflate(context, R.layout.onloading, null))
				.create();
				infoad = bulider.show();
				new Thread(item1_run).start();
			}
	}

	private class NewDataTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			try {
				Thread.sleep(1000);
				
			} catch (InterruptedException e) {
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			bulider = new AlertDialog.Builder(context);
			bulider.setView(View.inflate(context, R.layout.onloading, null))
			.create();
			infoad = bulider.show();
			new Thread(item1_run).start();
			mListView.completeRefreshing();
			super.onPostExecute(result);
		}
	}
}
